/**
 * @class   wosh::gui::WidgetSystemError
 * @brief   Shows Properties (PropertiesProvider) and allow user
 *          to edit then (asynch, remote through bus messages).
 *
 ****************************************************************************
 * @version $Id: WidgetSystemError.h 2184 2010-03-20 13:19:23Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_WidgetSystemError_H__
 #define __WOSH_Gui_WidgetSystemError_H__

#include <framework/gui/IWidgetSerializable.h>

#include <core/SystemError.h>

#include "ui_WidgetSystemError.h"

using namespace wosh;

namespace wosh {
 namespace gui {


class WidgetSystemError :  public QWidget, public virtual IWidgetSerializable  {

	Q_OBJECT

	public:

		WidgetSystemError(QWidget *parent = 0) : QWidget(parent) {
			this->m_ui = new Ui::WidgetSystemError();
			this->m_ui->setupUi(this);
			this->m_ui->wSourceURI->showLabel(false);
		 }
		virtual ~WidgetSystemError() {
			delete this->m_ui;
		 }

	public:
		virtual QWidget* asQWidget()					{ return this; }
		virtual const char* getSerializableType() const	{ return _SystemError_TYPE; }
		virtual ISerializable* newObject() const			{ return new SystemError(); }

		virtual WRESULT loadObject( const ISerializable* object ) {
			if ( object == NULL ) {
				this->m_ui->txtID->setText("");
				this->m_ui->txtTimeStamp->setText("");
				this->m_ui->wSourceURI->setTargetURI( URI() );
				this->m_ui->txtMethod->setText("");
				this->m_ui->spinErrorCode->setValue(0);
				this->m_ui->txtDescription->setText("");
				return WRET_OK;
			 }
			if ( !object->isClass(_SystemError_TYPE) ) return WRET_ERR_PARAM;
			const SystemError* syserr = dynamic_cast<const SystemError*>(object);
			if ( syserr == NULL ) return WRET_ERR_PARAM;

			this->setWindowTitle( QString("SystemError #%1").arg(syserr->getID()) );
			this->m_ui->txtID->setText( QString::number(syserr->getID()) );
			this->m_ui->txtTimeStamp->setText( QString::number(syserr->getTimeStamp()) );
			this->m_ui->wSourceURI->setTargetURI( syserr->getSource() );
			this->m_ui->txtMethod->setText( syserr->getMethod().c_str() );
			this->m_ui->spinErrorCode->setValue( syserr->getErrorCode() );
			this->m_ui->txtDescription->setText( syserr->getDescription().c_str() );

			return WRET_OK;
		 }

		virtual WRESULT saveObject( ISerializable* object ) const {
			if ( object == NULL ) return WRET_ERR_PARAM;
			if ( !object->isClass(_SystemError_TYPE) ) return WRET_ERR_PARAM;
			SystemError* syserr = dynamic_cast<SystemError*>(object);
			if ( syserr == NULL ) return WRET_ERR_PARAM;

			//syserr->setID( this->m_ui->txtID->text().toInt() );
			//this->m_ui->txtTimeStamp->text().toLatin1().data() )
			//this->m_ui->wSourceURI->setTargetURI();
			syserr->setMethod( this->m_ui->txtMethod->text().toLatin1().data() );
			syserr->setErrorCode( this->m_ui->spinErrorCode->value() );
			syserr->setDescription( this->m_ui->txtDescription->toPlainText().toLatin1().data() );

			return WRET_OK;
		 }
 
	protected:
		void changeEvent(QEvent *e) {
			QWidget::changeEvent(e);
			switch (e->type()) {
			case QEvent::LanguageChange:
				m_ui->retranslateUi(this);
				break;
			default:
				break;
			}
		}

		void closeEvent(QCloseEvent*) {
			 this->deleteLater();
		 }

	private:
		Ui::WidgetSystemError *m_ui;

}; // class def

 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_WidgetSystemError_H__
